
import config
import pymysql
def query_db(table_schema, table_name):
    conn = pymysql.connect(host=config.mysql_ip,   # 本地数据库
                              user=config.mysql_user,
                              password=config.mysql_password,
                              db=config.mysql_db,
                              charset=config.mysql_charset) #服务器名,账户,密码，数据库名称
    cursor = conn.cursor()
    sql = 'SELECT COLUMN_NAME, COLUMN_TYPE FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA=\''+table_schema+'\' and TABLE_NAME=\''+table_name+'\''
    cursor.execute(sql)
    data = list(cursor.fetchall())
    cursor.close()
    # print(data)
    return data

def write_to_file(dir_path, table_schema, table_name):
    data_list = query_db(table_schema, table_name)
    output = open(dir_path, 'w+')
    for col_name, col_type in data_list:
        print(col_name)
        output.write(col_name+" "+col_type+"\n")
        # print(data_list[i])
    output.close()







